<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("relationshipFunction.php");

$errMsg = "";
$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$dataArea = "";
$entity = "";
$sCondDataArea = "";
if(isset($_REQUEST["da"])){
	$dataArea = $_REQUEST["da"];
	if(!isset($aDataArea[$_REQUEST["da"]])){
		$errMsg .= _t("L'utente non &egrave; qualificato per vedere l'area dati")." \"".$_REQUEST["da"]."\"";
	}
}else if(isset($_REQUEST["data_area"])){
	$dataArea = $_REQUEST["data_area"];
	if(!isset($aDataArea[$_REQUEST["data_area"]])){
		$errMsg .= _t("L'utente non &egrave; qualificato per vedere l'area dati")." \"".$_REQUEST["data_area"]."\"";
	}
}else{
	$errMsg .= _t("L'area dati non &egrave; definita.");
}

if(isset($_REQUEST["ent"])){
	$entity = $_REQUEST["ent"];
}else if(isset($_REQUEST["entity"])){
	$entity = str_replace("'", "''", stripslashes($_REQUEST["entity"]));
}else{
	$errMsg .= _t("Entit&agrave; non definita").".<br>";
}

if(isset($_REQUEST["azione"]) && $_REQUEST["azione"] == "save"){
	//aggiornamento dati
	$errMsg .= entSubIns();
}
$discrAttribute = sxREQUEST("discr_attr");
$discrValue = sxREQUEST("discr_value");
$description = sxREQUEST("description");
$note = sxREQUEST("note");
$subtypeEnt = sxREQUEST("subtype_ent");
//leggo l'elenco delle entity disponibili
$sSQL = "SELECT * FROM entity ORDER BY entity";
if(!$objSDAO->query($sSQL, "sel", array("contOnErr"=>true))){
	$errMsg .= $objSDAO->error;
}

//output HTML
$aOpz = array("tit2"=>_t("Nuovo entit&agrave; sottotipo"), "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var objFrm = document.getElementById("objFrm");
		var objMessArea = document.getElementById("messArea");
		var errMsg = "";
		
		objTmp = document.getElementById("discr_attr");
		if(objTmp.value == ""){
			errMsg += "<?php echo _t("L'attributo discriminante &egrave; obbligatorio")?><br>";
			objTmp.className = "inpt01";
			objTmp.focus();
		}else{
			objTmp.className = "inpt01";
		}
		objTmp = document.getElementById("discr_value");
		if(objTmp.value == ""){
			errMsg += "<?php echo _t("Il valore discriminante &egrave; obbligatorio")?><br>";
			objTmp.className = "inpt01";
			objTmp.focus();
		}else{
			objTmp.className = "inpt01";
		}
		objTmp = document.getElementById("subtype_ent");
		if(objTmp.value == ""){
			errMsg += "<?php echo _t("L'entit&agrave; sottotipo &egrave; obbligatoria")?><br>";
			objTmp.className = "inpt01";
			objTmp.focus();
		}else{
			objTmp.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
//form inserimento dati
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"entitySubtypingIns.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"azione\" id=\"azione\" value=\"save\">");
echo("<input type=\"hidden\" name=\"entity\" id=\"entity\" value=\"$entity\">");
echo("<input type=\"hidden\" name=\"data_area\" id=\"data_area\" value=\"$dataArea\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
//output comandi
$aCmd = array("save"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Salva i dati"),
										"text"=>_t("salva"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"4\">"._t("Entit&agrave")." $entity: "._t("Sottotipizzazione")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("entit&agrave; supertipo")."</td>" .
			"<td class=\"text01\" colspan=\"3\">".str_replace($dataArea."_", "", $entity)."</td>" .
		"</tr>\n");
echo("<tr><td class=\"labl01\" width=\"15%\">"._t("attributo discriminante")."*</td>" .
				"<td class=\"text01\" width=\"35%\">");
echo("<select class=\"inpt01\" name=\"discr_attr\" id=\"discr_attr\">");		
echo("<option value=\"\">----</option>");
$sSQL = "SELECT * FROM attribute WHERE entity='".$entity."'";
if($objSDAO->query($sSQL, "selAttrPar", array("contonErr"=>true))){
	while($row = $objSDAO->fetch("selAttrPar")){
		$chk = "";
		if($row["attribute"] == $discrAttribute)
				$chk = "SELECTED";
		echo("<option value=\"".$row["attribute"]."\" $chk>".$row["attribute"]."</option>");	
	}
}else{
	$errMsg .= $objSDAO->error;
}
echo("</select>");
echo("</td>");
echo("</tr>\n");
echo("<tr>");
echo("<td class=\"labl01\" width=\"25%\">"._t("valore discriminante")."*</td>");
echo("</td>");
echo("<td class=\"text01\" width=\"75%\"><input type=\"text\" maxlenght=\"32\" size=\"20\" name=\"discr_value\" id=\"discr_value\" value=\"".sxHE($discrValue)."\"></td>");
echo("</td>");
echo("</tr>\n");
echo("<tr>");
echo("<td class=\"labl01\">"._t("descrizione valore discriminante")."</td>");
echo("</td>");
echo("<td class=\"text01\"><input class=\"inpt01\" type=\"text\" size=\"40\" name=\"description\" id=\"description\" value=\"".sxHE($description)."\"></td>");
echo("</td>");
echo("</tr>\n");
echo("<tr>" .
				"<td class=\"labl01\">"._t("Entit&agrave; sottotipo")."*</td>" .
				"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"subtype_ent\" id=\"subtype_ent\">");
echo("<option value=\"\">----</option>");
while($row = $objSDAO->fetch("sel")){
	$chk = "";
	if($row["entity"] == $subtypeEnt)
		$chk = "SELECTED";
	echo("<option value=\"".$row["entity"]."\" $chk>".str_replace($row["data_area"]."_", "", $row["entity"])."</option>");	
}
echo("</select>");
echo("</td></tr>");
echo("<tr>");
echo("<td class=\"labl01\">"._t("note")."</td>");
echo("</td>");
echo("<td class=\"text01\"><textarea  class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"8\">".sxHE($note)."</textarea></td>");
echo("</td>");
echo("</tr>\n");	
echo("</table>");

echo(commandsHTML($aCmd));
echo("</form>\n");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
$objSDAO->close();
?>
